//************************************************************
//  Copyright (c) 深圳市赛元微电子有限公司
//	文件名称	: tuya_wifi.c
//	模块功能	: 涂鸦IOT WIFI驱动 中断函数C文件
//  说明        :  
//*************************************************************
#include "SC_Init.h"
#include "..\H\SCD_tuyawifi.h"
//====================================
#define STR1(R) #R
#define STR2(R) STR1(R)
#define PRODUCT_KEY STR2(WIFI_TUYA_ID)

#define CONFIG_MODE STR2(INTERNET_MODE)

#define MCU_VER		STR2(WIFI_SETMCU_VER)

#define PRODUCTS_INF   "{\"p\":\"" PRODUCT_KEY "\",\"v\":\"" MCU_VER "\",\"m\":" CONFIG_MODE "}"


#if  WIFI_UART==0		//UART0	(92F742X 无效)
sfr   WIFI_SBUF    = 0X99;  	//串口数据缓存寄存器
sfr   WIFI_TI      = 0X98;   	//串口控制寄存器
sfr   WIFI_RI      = 0X98;   	//串口控制寄存器
#endif

#if  WIFI_UART==1		//92FXXXX、93FXXXX  UART1 
sfr   WIFI_SBUF    = 0X9F;  	//串口数据缓存寄存器 
sfr   WIFI_TI      = 0X9D;   	//串口控制寄存器     
sfr   WIFI_RI      = 0X9D;   	//串口控制寄存器     
#endif

#if  WIFI_UART==2				//95FXXXX UART1 
sfr   WIFI_SBUF    = 0X9F;  	//串口数据缓存寄存器 
sfr   WIFI_TI      = 0X95;   	//串口控制寄存器     
sfr   WIFI_RI      = 0X95;   	//串口控制寄存器     
#endif

//=================================
unsigned char code 	WifiPID_CODE[]=PRODUCTS_INF;
unsigned char WifiCurren_Mcu_Ver[]=MCU_VER;
bit 			WifiUartSendFlag = 0;			 ////uart 发送完成标志
unsigned char 	WifiUart_Data=0;

unsigned char 	WifiData[WIFI_LENGH_MAX] = {0};
unsigned int 	WifiLengh=0;
unsigned char	WifiDPid=0;
unsigned char	WifiDPtype=0;
unsigned char   WifiCommand=0;
unsigned long   WifiValue=0;
//=================================
unsigned char code *weather_choose[] = {
    "temp",
    "humidity",
    "condition",
		
    "pm25",/*
    "pressure",
    "realFeel",
    "uvi",
    "tips",
    "windDir",
    "windLevel",
    "windSpeed",
    "sunRise",
    "sunSet",
    "aqi",
    "so2 ",
    "rank",
    "pm10",
    "o3",
    "no2",
    "co",
    */
};	
unsigned char weather_len = sizeof(weather_choose) / sizeof(weather_choose[0]); 
/*****************************************************
*函数名称: tuyawifi_uartinterrupt_service
*函数功能: UART 中断函数
*入口参数：void
*出口参数：void
*说明    ：该函数必须放在相应的UART中断函数中
*****************************************************/
void tuyawifi_uartinterrupt_service(void)
{
    if((WIFI_TI&0x02)!=0)
    {
		  #if defined(SC95F8x3x) || defined(SC95F7x3x)
      WIFI_TI |= 0x02;
		  #else
      WIFI_TI &= ~0x02;
	    #endif
      WifiUartSendFlag = 1;		
    }
    if((WIFI_RI&0x01)!=0)
    {
    	WifiUart_Data=WIFI_SBUF;
      uart_receive_input_frame();
		  #if defined(SC95F8x3x) || defined(SC95F7x3x)
		  WIFI_RI |= 0x01;      
	    #else
		  WIFI_RI &= (~0x01);
	    #endif		
    }
}
/*****************************************************
*函数名称: transfer_uart
*函数功能: UART 发送函数
*入口参数：Value
*出口参数：void
*****************************************************/
void	transfer_uart(unsigned char Value)
{
	unsigned int i;

	WifiUartSendFlag = 0;
	WIFI_TI&=~0x02;
	WIFI_SBUF=Value;
	for(i=0;i<5000;i++)			//超时
	{
		WDTCON=0X10;
		if(WifiUartSendFlag!=0||((WIFI_TI&0x02)!=0))
		{		
			break;
		}
	}
}